import { IEventHandler, IRenderReplyMessage } from '../EventHandler'

import fs from 'fs'
import path from 'path'
import { IWriteTextFile } from '../../declare/Message'

export class FileTextWriter implements IEventHandler<IWriteTextFile, void> {
    handle(messageId: string, params?: IWriteTextFile): IRenderReplyMessage<void> {
        try {
            fs.writeFileSync(path.join(params?.path, params?.filename), params.textData)
            return { isSuccess: true, messageId: messageId }
        } catch (err) {
            return { isSuccess: false, messageId: messageId, data: err }
        }
    }
}
